{% extends 'base.html' %}
{% load widget_tweaks %}

{% block title %}学院管理{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2><i class="fas fa-university"></i> 学院管理</h2>
                <a href="{% url 'accounts:college_create' %}" class="btn btn-primary">
                    <i class="fas fa-plus"></i> 创建学院
                </a>
            </div>

            <!-- 搜索表单 -->
            <div class="card mb-4">
                <div class="card-body">
                    <form method="get" class="row g-3">
                        <div class="col-md-8">
                            <input type="text" name="search" class="form-control" 
                                   placeholder="搜索学院名称、代码或描述..." 
                                   value="{{ search }}">
                        </div>
                        <div class="col-md-4">
                            <button type="submit" class="btn btn-outline-primary me-2">
                                <i class="fas fa-search"></i> 搜索
                            </button>
                            {% if search %}
                            <a href="{% url 'accounts:college_list' %}" class="btn btn-outline-secondary">
                                <i class="fas fa-times"></i> 清除
                            </a>
                            {% endif %}
                        </div>
                    </form>
                </div>
            </div>

            <!-- 统计信息 -->
            <div class="row mb-4">
                <div class="col-md-3">
                    <div class="card bg-primary text-white">
                        <div class="card-body">
                            <div class="d-flex justify-content-between">
                                <div>
                                    <h4>{{ total_count }}</h4>
                                    <p class="mb-0">学院总数</p>
                                </div>
                                <div class="align-self-center">
                                    <i class="fas fa-university fa-2x"></i>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 学院列表 -->
            <div class="card">
                <div class="card-body">
                    {% if page_obj %}
                    <div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead class="table-dark">
                                <tr>
                                    <th>学院名称</th>
                                    <th>学院代码</th>
                                    <th>所属学部</th>
                                    <th>描述</th>
                                    <th>关联用户</th>
                                    <th>关联子项目</th>
                                    <th>创建时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for college in page_obj %}
                                <tr>
                                    <td>
                                        <strong>{{ college.name }}</strong>
                                    </td>
                                    <td>
                                        <span class="badge bg-info">{{ college.code }}</span>
                                    </td>
                                    <td>
                                        {% if college.department %}
                                            <span class="badge bg-primary">{{ college.department.get_name_display }}</span>
                                        {% else %}
                                            <span class="text-muted">未分配</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if college.description %}
                                            {{ college.description|truncatechars:50 }}
                                        {% else %}
                                            <span class="text-muted">无描述</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <span class="badge bg-secondary">{{ college.users.count }}</span>
                                    </td>
                                    <td>
                                        <span class="badge bg-secondary">{{ college.subprojects.count }}</span>
                                    </td>
                                    <td>
                                        <small class="text-muted">{{ college.created_at|date:"Y-m-d H:i" }}</small>
                                    </td>
                                    <td>
                                        <div class="btn-group btn-group-sm" role="group">
                                            <a href="{% url 'accounts:college_detail' college.pk %}" 
                                               class="btn btn-outline-info" title="查看详情">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <a href="{% url 'accounts:college_update' college.pk %}" 
                                               class="btn btn-outline-warning" title="编辑">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <a href="{% url 'accounts:college_delete' college.pk %}" 
                                               class="btn btn-outline-danger" title="删除">
                                                <i class="fas fa-trash"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>

                    <!-- 分页 -->
                    {% if page_obj.has_other_pages %}
                    <nav aria-label="学院列表分页">
                        <ul class="pagination justify-content-center">
                            {% if page_obj.has_previous %}
                                <li class="page-item">
                                    <a class="page-link" href="?page=1{% if search %}&search={{ search }}{% endif %}">首页</a>
                                </li>
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search %}&search={{ search }}{% endif %}">上一页</a>
                                </li>
                            {% endif %}

                            {% for num in page_obj.paginator.page_range %}
                                {% if page_obj.number == num %}
                                    <li class="page-item active">
                                        <span class="page-link">{{ num }}</span>
                                    </li>
                                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                                    <li class="page-item">
                                        <a class="page-link" href="?page={{ num }}{% if search %}&search={{ search }}{% endif %}">{{ num }}</a>
                                    </li>
                                {% endif %}
                            {% endfor %}

                            {% if page_obj.has_next %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search %}&search={{ search }}{% endif %}">下一页</a>
                                </li>
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if search %}&search={{ search }}{% endif %}">末页</a>
                                </li>
                            {% endif %}
                        </ul>
                    </nav>
                    {% endif %}
                    {% else %}
                    <div class="text-center py-5">
                        <i class="fas fa-university fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">暂无学院数据</h5>
                        <p class="text-muted">点击上方"创建学院"按钮添加第一个学院</p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}